<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../include/tag.jsp" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <title>推荐关系绑定</title>
    <%@include file="../include/commonFile.jsp" %>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/common/formSelects-v4.css">
    <link rel="stylesheet" href="${ctx}/css/ui/activity/publish_form.css">
    <script type="text/javascript" src="${ctxStatic}/typeahead/typeahead.bundle.js"></script>
    <style type="text/css">
        .index-outside {
            min-height: auto !important;
        }

        .index-outside > section .section-main {
            min-width: auto !important;
        }
    </style>

    <style>
        .member-cell {
            display: -webkit-box;
            display: -webkit-flex;
            display: -moz-box;
            display: -ms-flexbox;
            display: flex;
            width: 100%;
            -webkit-box-align: center;
            -webkit-align-items: center;
            -moz-box-align: center;
            -ms-flex-align: center;
            align-items: center;
            -webkit-box-pack: start;
            -webkit-justify-content: flex-start;
            -moz-box-pack: start;
            -ms-flex-pack: start;
            justify-content: flex-start;
        }

        .member-cell .member-logo {
            display: inline-block;
            /*float: left;*/
            width: 30px;
            height: 30px;
            -webkit-border-radius: 50%;
            border-radius: 50%;
            line-height: 50px;
            -webkit-background-size: cover;
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
        }

        .member-cell .member-name {
            -webkit-box-flex: 1;
            -moz-box-flex: 1;
            -ms-flex: 1;
            -webkit-flex: 1;
            flex: 1;
            line-height: 30px;
            padding-left: 5px;
            display: inline-block;
            text-align: left;
        }

        .member-cell .member-logo-img {
            width: 30px;
            height: 30px;
            -webkit-border-radius: 50%;
            border-radius: 50%;
        }

        .layui-form-label {
            width: 75px !important;
        }
    </style>
</head>
<body>
<div class="index-outside">
    <!--内容-->
    <section class="no-fix">
        <div class="section-main">
            <!-- 正文请写在这里 -->
            <div class="add-form-content">
                <form id="myForm" class="layui-form mt20" method="post"  action="${ctx}/member/membermgmt/bindingRR.do">
                    <input type="hidden" name="cuserId" value="${cUserIntegral.id}"/>
                    <input type="hidden" name="memberId" value="${memberId}"/>
                    <input type="hidden" id="tjMemberId" name="tjMemberId" value="${tjMember.id}">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">推荐人</label>
                            <c:if test="${tjMember!=null}">
                                <div class="member-cell layui-input-inline">
                                    <div class="member-logo common-img">
                                        <img src="<c:if test="${tjMember.logo!= null}">${tjMember.logo}?imageMogr2/auto-orient/crop/126x126</c:if>"
                                             alt="" onerror="txz.errImg(this)"
                                             onload="txz.imgLoad(this)">
                                    </div>
                                    <div class="member-name ellipsis-1">
                                        <span class="blue" >${tjMember.realname}</>
                                    </div>
                                </div>
                            </c:if>
                            <c:if test="${tjMember==null}">
                                <div class="layui-input-inline">
                                    <input class="layui-input" id="tjMemberMobile"
                                           name="tjMemberMobile"
                                           placeholder="请输入推荐人手机号"
                                           value="">
                                </div>
                                <div class="layui-form-mid layui-word-aux">推荐关系绑定后不可修改</div>
                            </c:if>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
<%--                            <label class="layui-form-label">分类</label>
                            <div class="layui-input-inline">
                                <select name="integralTypeId" lay-filter="types" id="types">
                                    <option value="请选择"></option>
                                    <c:forEach var="type" items="${cIntegralTypes}">
                                        <option value="${type.id}" ${type.id == cUserIntegral.integralTypeId ? 'selected="selected"' : ''}>${type.name}</option>
                                    </c:forEach>
                                </select>
                            </div>
                            <div style="height: 50px"></div>--%>
                            <label class="layui-form-label">积分项</label>
                            <div class="layui-input-inline" style="width: 340px">
                                <select name="integralItemId" lay-filter="items" id="items">
                                    <option value="请选择"></option>
                                    <c:forEach var="item" items="${cIntegralItems}">
                                        <option value="${item.id}" fractionVal = "${item.itemFraction}" ${cUserIntegral.integralItemId == item.id ? 'selected="selected"' : ''}>${item.name}</option>
                                    </c:forEach>
                                </select>
                            </div>
                            <div style="height: 50px"></div>
                            <label class="layui-form-label">分数 :</label>
                            <div class="layui-input-inline" style=" margin-top: 3px;">
                                <span class="active-red" id="itemFractionVal" style="font-size: 20px"></span>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </section>
</div>
<script>
    var form, laydate, formcache;
    var thSel_tj = false, thMember_tj = {};
    $(function () {
        layui.use(['form', 'laydate'], function () {
            form = layui.form, laydate = layui.laydate;
            //自定义验证规则
            form.verify({
                name: function (value) {
                    if (value.trim() == "") {
                        return '分类名不能为空';
                    }
                },
                itemFraction: function (value) {
                    if (value.trim() == "") {
                        return "请填写分数";
                    }
                    if (value <= 0) {
                        return "请填写正确的分数";
                    }
                }
            });

            // 积分项
            form.on('select(items)', function (data) {
                $("#itemFractionVal").html("")
                var itemFractionVal = $(data.elem).find("option:selected").attr("fractionVal");
                $("#itemFractionVal").html(itemFractionVal);
            });

            if ('${not empty cIntegralItem.id}' == 'true') {
                var itemFraction = '${cIntegralItem.itemFraction}';
                $("#itemFractionVal").html(itemFraction);
            }
        });
    });

    function doSubmit(data) {
        var result = true;
        $.ajax({
            type: 'POST',
            async: false, // 使用同步的方法
            data: $('#myForm').serialize(),
            dataType: 'json',
            success: function (res) {
                if (res.success) {
                    util.layerMsgSuccess("操作成功")
                } else {
                    util.layerMsgError(res.description);
                    result = false;
                }
            },
            url: $("#myForm").attr("action")
        });

        return result;
    }

    function getFormData() {
        var formData = util.serializeForm($('#myForm').serializeArray());
        return formData;
    }
    function openDialogShow(title, url, width, height, target) {
        layer.open({
            type: 2,
            area: [width, height],
            title: title,
            maxmin: false, // 开启最大化最小化按钮
            content: url,
            btn: ['关闭'],
            yes: function (index, layero) {
                setTimeout(function () {
                    top.layer.close(index);
                }, 100);// 延时0.1秒，对应360 7.1版本bug
            }
        });
    }

    $('#tjMemberMobile').typeahead({
        hint: false,
        highlight: true,
        minLength: 1
    }, {
        name: 'tjMemberMobile',
        display: 'tjMemberMobile',
        async: true,
        templates: {
            suggestion: function (member) {
                return '<a href="javascript:void(0);"><span data-id="' + member.id + '" class="t-user-logo" style="background-image: url(' + member.logo
                    + '),url(${ctx}/image/def_user_logo.png)"></span><span class="t-user-name">' + (member.realname || '') + (member.mobile || '') + '</span></a>';
            }
        },
        source: function (q, syn, aja) {
            _findPartnerMember('phone', q, aja);
        }
    });

    $('#tjMemberMobile').bind('typeahead:select', function (ev, suggestion) {
        thMember_tj = suggestion;
        thSel_tj = true;
        $(ev.currentTarget).blur();
    });
    $('#tjMemberMobile').blur(function () {
        if (thSel_tj) {
            $('#tjMemberId').val(thMember_tj.id);
            $('#tjMemberMobile').val(thMember_tj.mobile);

        } else {
            if (thMember_tj.id) {
                thMember_tj = {};
                $('#tjMemberId').val("");
                $('#tjMemberMobile').val("");
            }
        }
        form.render();
        thSel_tj = false;
    });
    function _findPartnerMember(type, q, cb) {
        $.ajax({
            url: "${ctx}/system/member/findPartnerMemberByPhoneOrName.do",
            type: "post",
            dataType: "json",
            data: {
                property: type,
                value: q,
                pmId: $("input[name=pmId]").val()
            },
            success: function (ret) {
                cb(ret.data);
            }
        });
    }
</script>
</body>
</html>